<?php
class Admin extends CI_Controller{
    
    function __construct() {
        parent::__construct();
        $this->load->model('model_user');
        if($this->model_user->auth() == 0 || $this->model_user->auth() !== 1){
            redirect('login/index');
        }else{
            if ($this->session->userdata['type'] == 'boss') {
                redirect('boss');
            } else if ($this->session->userdata['type'] == 'pegawai1') {
                redirect('pegawai1');
            } else if($this->session->userdata['type'] == 'pegawai2'){
                redirect('pegawai2');
            }
        }
    }
    
    function index(){
        $data['nama'] = $this->session->userdata['nama'];
        $data['main_content'] = 'welcome';
        $this->load->view('includes/template',$data);
    }
    
    function boss(){
        $data['bosses'] = $this->model_user->fetch_user('boss');
        $data['nama'] = $this->session->userdata['nama'];
        $data['fetch_type'] = 'boss';
        $data['main_content'] = 'user/home';
        $this->load->view('includes/template',$data);
    }
    
    function pegawai1(){
        $data['pegawai1s'] = $this->model_user->fetch_user('pegawai1');
        $data['nama'] = $this->session->userdata['nama'];
        $data['fetch_type'] = 'pegawai1';
        $data['main_content'] = 'user/home';
        $this->load->view('includes/template',$data);
    }
    
    function pegawai2(){
        $data['pegawai2s'] = $this->model_user->fetch_user('pegawai2');
        $data['nama'] = $this->session->userdata['nama'];
        $data['fetch_type'] = 'pegawai2';
        $data['main_content'] = 'user/home';
        $this->load->view('includes/template',$data);
    }
    
    function refresh($level){
        switch($level){
            case 'boss':
                $data['bosses'] = $this->model_user->fetch_user('boss');
                $this->load->view('user/showboss',$data);
                break;
            case 'pegawai1':
                $data['pegawai1s'] = $this->model_user->fetch_user('pegawai1');
                $this->load->view('user/showpegawai1',$data);
                break;
            case 'pegawai2':
                $data['pegawai2s'] = $this->model_user->fetch_user('pegawai2');
                $this->load->view('user/showpegawai2',$data);
                break;
        }
    }
    
    function input($usertype){
        $data['usertype'] = $usertype;
        $this->load->view('user/input',$data);
    }
    
    function cek_nama(){
        $q = $this->input->post('q');
        echo $this->model_user->cek_nama($q);
    }
    
    function cek_username(){
        $q = $this->input->post('q');
        echo $this->model_user->cek_username($q);
    }
    
    function submit(){
        if($this->model_user->insert_user()){
            echo "<script type='text/javascript'>window.top.window.alertz('alert-success','Sukses !', 'Data berhasil dimasukkan', '".$this->input->post('level')."');</script>";
        }else{
            echo "<script type='text/javascript'>window.top.window.alertz('alert-error','Gagal !', 'Data gagal masuk', '".$this->input->post('level').");</script>";
        }
    }
    
    function update(){
        if($this->model_user->update_user()){
            echo "<script type='text/javascript'>window.top.window.alertz('alert-success','Sukses !', 'Data berhasil diupdate', '".$this->input->post('level')."');</script>";
        }else{
            echo "<script type='text/javascript'>window.top.window.alertz('alert-error','Gagal !', 'Data gagal diupdate', '".$this->input->post('level')."');</script>";
        }
    }
    
    function edit($id){
        $data['row'] = $this->model_user->fetch_single($id);
        $this->load->view('user/edit',$data);
    }
    
    function delconf($id){
        $data['row'] = $this->model_user->fetch_single($id);
        $this->load->view('user/delconf',$data);
    }
    
    function delete(){
        if($this->model_user->delete_user()){
            echo "<script type='text/javascript'>window.top.window.alertz('alert-success','Sukses !', 'Data berhasil dihapus', '".$this->input->post('level')."');</script>";
        }else{
            echo "<script type='text/javascript'>window.top.window.alertz('alert-error','Gagal !', 'Data gagal dihapus', '".$this->input->post('level')."');</script>";
        }
    }
    
    function setting(){
        $data['row'] = $this->model_user->fetch_single($this->session->userdata('id'));
        $this->load->view('setting',$data);
    }
    
    function update_set(){
        if($this->model_user->update_set()){
            echo "<script type='text/javascript'>window.top.window.alertu('Setting berhasil diupdate');</script>";
        }else{
            echo "<script type='text/javascript'>window.top.window.alertu('Setting gagal diupdate');</script>";
        }
    }
    
    function search($level){
        $q = $this->input->post('q');
        $field = $this->input->post('field');
        switch($level){
            case 'boss':
                $data['bosses'] = $this->model_user->search('boss',$q,$field);
                $this->load->view('user/showboss',$data);
                break;
            case 'pegawai1':
                $data['pegawai1s'] = $this->model_user->search('pegawai1',$q,$field);
                $this->load->view('user/showpegawai1',$data);
                break;
            case 'pegawai2':
                $data['pegawai2s'] = $this->model_user->search('pegawai2',$q,$field);
                $this->load->view('user/showpegawai2',$data);
                break;
        }
    }
}
?>
